/**
 * 
 */
package com.jm.utils;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletContext;

import com.jm.web.data.BaseDataEntity;
import com.jm.wf.data.PanTable;
import com.jm.wf.data.WFStep;
import com.jm.wf.data.WorkFlow;

/**
 * @author yanjie
 * Dec 14, 2011 9:48:20 AM
 */
public class GlobalHelper {
	public static final String TAG_GlobalHelper = "JM.GLOBALHELPER";
	public static final String VAR_HOLIDAY = "holiday.xml";//國假
	private ServletContext servletContext = null;
	protected Map<String,BaseDataEntity> compMap = null;//
	protected Map<String,BaseDataEntity> ipMap = null;//
	protected Map<String,WorkFlow> wfMap = null;//
	protected Map<String,Map<String,BaseDataEntity>> subProMap = null;//
	protected Map<String,BaseDataEntity> subProIPMap = null;//
	
	public GlobalHelper(ServletContext context) {
		this.servletContext = context;
	}
	
	public void init() {
	}
	
	public void load(){
		//載入流程
		reloadWorkFlow();
		
	}
	
	public WorkFlow getWorkFlow(String wfdataSource,String wfname){
		if(this.wfMap == null 
				|| StringHelper.IsNullOrEmpty(wfdataSource)
				|| StringHelper.IsNullOrEmpty(wfname)) return null;
		return this.wfMap.get(wfdataSource+wfname);
	}
	
	public void reloadWorkFlow(){
		if(this.wfMap == null)this.wfMap = new HashMap<String,WorkFlow>();
		this.wfMap.clear();
		reloadWorkFlow("pan");
	}
	/**
	 * 載入流程
	 * 后面要優化 ;
	 */
	public void reloadWorkFlow(String strDataSource){
		Utils utils = null;
		
		try{
			utils = new Utils(null);
			ResultSet rs = null;
			Vector vector = new Vector();
			//1.info
			String sql = "select * from wf..wf001 where enab = 1 ";
			rs = utils.executeQuery(strDataSource,sql);
			while(rs.next()){
				WorkFlow wf = new WorkFlow();
				wf.FromResultSet(rs,false);
				
				ResultSet rs2 = utils.executeQuery(utils.getRsString(rs,"wfdatasource"), "select * from wf..wf002 where wf001sn = '"+utils.getRsString(rs, "sn")+"'");
				List<WFStep> list = new ArrayList<WFStep>();
				while(rs2.next()){
					WFStep wfStep = new WFStep();
					wfStep.FromResultSet(rs2, false);
					list.add(wfStep);
				}
				rs2.close();
				
				ResultSet rs3 = utils.executeQuery("info5", "select * from pan..pan_table where sn = '"+utils.getRsString(rs, "ptablesn")+"'");
				if(rs3.next()){
					PanTable panTable = new PanTable();
					panTable.FromResultSet(rs3, false);
					wf.setPanTable(panTable);
				}
				rs3.close();
				
				wf.setWfsteps(list);
				wfMap.put(utils.getRsString(rs,"wfdatasource")+utils.getRsString(rs,"wfname"),wf);
			}
			rs.close();
	        System.out.println("流程加載完成!");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			utils.cleanUp();
		}
	}
	
	private String getDataPath() {
		String realpath = this.getServletContext().getRealPath("");
    	StringBuffer filePath = new StringBuffer(realpath.substring(0, realpath.lastIndexOf("\\")));
    	
    	filePath.append("\\userData\\config\\data\\");
        return filePath.toString();
	}

	public ServletContext getServletContext() {
		return servletContext;
	}
}
